package com.ruoyi.web.controller.app.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.base.XException;
import com.ruoyi.sj.domain.Order;
import com.ruoyi.sj.mapper.MoneyMapper;
import com.ruoyi.sj.mapper.OrderMapper;
import com.ruoyi.util.SerialNumber;
import com.ruoyi.web.controller.pay.PayComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName AppCodePayService
 * @Description TODO
 * @Author tyc@yanyu
 * @Date 2019/1/2 0002 16:38
 **/
@Service
public class AppCodePayService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private PayComponent payComponent;
    @Autowired
    private MoneyMapper moneyMapper;

    private SerialNumber serial = new SerialNumber("SK");
    private SerialNumber serial2 = new SerialNumber("FK");
    private SerialNumber serial3 = new SerialNumber("ZZ");

    /**
     * 二维码收付款
     *
     * @param memberId
     * @param
     * @param type
     * @return
     */
    public Object payMoney(String memberId, String userId, String payPassword, String money,String title) {
        if (StringUtils.isBlank(memberId)) {
            throw new XException("用户ID号不能为空");
        }
        if (StringUtils.isBlank(money)) {
            throw new XException("金额不能为空");
        }
        Order order = new Order();
        String atomicNumber = serial.getAtomicNumber();
        order.setMemberId(userId);
        order.setSn(atomicNumber);
        order.setGoodsId(memberId);
        order.setTitle(title);
        order.setPayMoney(money);
        order.setPayType("balance");
        order.setType(6);
        order.setPayPassword(payPassword);
        payComponent.sendToPay(order);
        payComponent.hanldeSuccess(order);
        return order;
    }

    /**
     * 付款
     *
     * @param memberId
     * @param userId
     * @param money
     * @return
     */
    public Object acceptCode(String userId, String money) {
        if (StringUtils.isBlank(money)) {
            throw new XException("金额不能为空");
        }
        Order order = new Order();
        String atomicNumber = serial2.getAtomicNumber();
        order.setSn(atomicNumber);
        order.setGoodsId(userId);
        order.setTitle("收款");
        order.setPayMoney(money);
        order.setPayType("balance");
        order.setType(7);
        // 修改订单状态为未支付
        order.setStatus(0);
        // 创建订单时间
        order.setCreateDate(new Date());
        order.setUpdateTime(new Date());
        orderMapper.insertOrder(order);
        return order;
    }

    /**
     * 付款
     *
     * @param memberId
     * @param userId
     * @param money
     * @return
     */
    public Object acceptMoney(String orderSn, String userId, String money) {
        if (StringUtils.isBlank(orderSn)) {
            throw new XException("订单号不能为空");
        }
        if (StringUtils.isBlank(money)) {
            throw new XException("金额不能为空");
        }
        Order order = orderMapper.selectBySn(orderSn);
        order.setMemberId(userId);
        if (order != null) {
            if (order.getStatus() == 0) {
                if (!StringUtils.equals(order.getGoodsId(), order.getMemberId())) {
                    payComponent.sendToPay(order);
                    payComponent.hanldeSuccess(order);
                } else {
                    throw new XException("不能跟自己付款");
                }
            } else {
                throw new XException("付款码已经失效");
            }
        } else {
            throw new XException("付款码不存在");
        }
        return order;
    }

    /**
     * 转账
     *
     * @param memberId
     * @param userId
     * @param money
     * @return
     */
    public Object transferById(String memberId, String userId, String money, String payPassword,String title) {
        if (StringUtils.isBlank(memberId)) {
            throw new XException("转账人ID不能为空");
        }
        if (StringUtils.isBlank(userId)) {
            throw new XException("收款人ID不能为空");
        }
        if (StringUtils.isBlank(money)) {
            throw new XException("金额不能为空");
        }
        Order order = new Order();
        String atomicNumber = serial3.getAtomicNumber();
        order.setMemberId(memberId);
        order.setSn(atomicNumber);
        order.setGoodsId(userId);
        order.setTitle(title);
        order.setPayMoney(money);
        order.setPayType("balance");
        order.setCreateDate(new Date());
        order.setType(0);
        order.setPayPassword(payPassword);
        payComponent.sendToPay(order);
        return order;
    }

    /**
     * 收账
     *
     * @param orderSn
     * @param userId
     * @return
     */
    public Object transferMoney(String orderSn, String userId) {
        if (StringUtils.isBlank(orderSn)) {
            throw new XException("订单号不能为空");
        }
        if (StringUtils.isBlank(userId)) {
            throw new XException("用户ID号不能为空");
        }
        Order order = orderMapper.selectBySn(orderSn);
        if (null != order) {
            if (userId.equals(order.getGoodsId())) {
                payComponent.hanldeSuccess(order);
            }else {
                throw new XException("无效转账");
            }
        } else {
            throw new XException("转账不存在");
        }
        return order;
    }


}
